package top.saplf.sileny.player.home

import android.util.Base64
import android.view.View
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.github.ajalt.timberkt.Timber
import com.jakewharton.rxbinding2.view.clicks
import com.jakewharton.rxbinding2.view.longClicks
import kotlinx.android.synthetic.main.item_local_music_index.view.*
import top.saplf.sileny.R

/**
 * @author saplf
 */
class LocalMusicIndexAdapter : BaseQuickAdapter<Music, BaseViewHolder>(R.layout.item_local_music_index) {
    var itemAction: ((Music, View) -> Unit)? = null
    var longItemAction: ((Music) -> Unit)? = null

    fun getSharedElement(musicId: String): View {
        val position = data.indexOfFirst { it.id == musicId }
        return getViewByPosition(position, R.id.cover)
    }

    override fun convert(helper: BaseViewHolder, item: Music) {
        with(helper.itemView) {
            name.text = item.name
            author.text = "${item.author} | ${item.album}"
            Glide.with(context)
                    .load(Base64.decode(item.cover, Base64.DEFAULT))
                    .into(cover)
            cover.transitionName = item.id
            rootView.clicks()
                    .subscribe { itemAction?.invoke(item, cover) }
            rootView.longClicks()
                    .subscribe { longItemAction?.invoke(item) }
        }
    }
}